//
//  TestViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/5/28.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "TestViewController.h"

@interface TestViewModel : NSObject
@property (strong, nonatomic) RACSubject *subject;

-(void)loadData;
@end
@implementation TestViewModel
-(void)loadData{
    // request service data,return data to vc
    [self.subject sendNext:@1];
}

-(RACSubject *)subject{
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}
@end

@interface TestViewController ()
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *sendSingnalButton;

@property (strong, nonatomic) TestViewModel *viewModel;
@end

@implementation TestViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [[RACScheduler mainThreadScheduler] afterDelay:1 schedule:^{
            [subscriber sendNext:@1];
        }];
        [[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{
            [subscriber sendNext:@2];
        }];
        [[RACScheduler mainThreadScheduler] afterDelay:3 schedule:^{
            [subscriber sendNext:@3];
        }];
        [[RACScheduler mainThreadScheduler] afterDelay:4 schedule:^{
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    NSLog(@"Signal was created.");
    
    [[RACScheduler mainThreadScheduler] afterDelay:1 schedule:^{
        [signal subscribeNext:^(id  _Nullable x) {
            NSLog(@"Subscriber 1 recveive: %@", x);
        }];
    }];
    [[RACScheduler mainThreadScheduler] afterDelay:2.1 schedule:^{
        [signal subscribeNext:^(id x) {
            NSLog(@"Subscriber 2 recveive: %@", x);
        }];
    }];
}
-(TestViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [TestViewModel new];
    }
    return _viewModel;
}
@end
